/**
 *  场景类
 *  基础API操作
 *  
 */
class BaseScene {
    //当前所有Layer
    private layers: Array<egret.DisplayObjectContainer>;

    /**
     * 构造函数
     */
    public constructor() {
        this.layers = new Array<egret.DisplayObjectContainer>();
    }

    /**
     * 进入Scene调用
     */
    public onEnter(): void {

    }

    /**
     * 退出Scene调用
     */
    public onExit(): void {
        //TODO 退出场景
        this.removeAllLayer();
    }

    /**
     * 添加一个Layer到舞台
     * @param layer
     */
    public addLayer(layer: egret.DisplayObjectContainer): void {
        if(layer instanceof BaseSpriteLayer) {
            StageScene.get.getStage().addChild(layer);
            this.layers.push(layer);
        }
        else if(layer instanceof BaseEuiLayer) {
            StageScene.get.getUIStage().addChild(layer);
            this.layers.push(layer);
        }
    }

    /**
     * 添加一个Layer到舞台
     * @param layer
     */
    public addLayerAt(layer: egret.DisplayObjectContainer,index: number): void {
        if(layer instanceof BaseSpriteLayer) {
            StageScene.get.getStage().addChildAt(layer,index);
            this.layers.push(layer);
        }
        else if(layer instanceof BaseEuiLayer) {
            StageScene.get.getUIStage().addChildAt(layer,index);
            this.layers.push(layer);
        }
    }

    /**
     * 在舞台移除一个Layer
     * @param layer
     */
    public removeLayer(layer: egret.DisplayObjectContainer): void {
        if(layer instanceof BaseSpriteLayer) {
            StageScene.get.getStage().removeChild(layer);
            this.layers.splice(this.layers.indexOf(layer),1);
        }
        else if(layer instanceof BaseEuiLayer) {
            StageScene.get.getUIStage().removeChild(layer);
            this.layers.splice(this.layers.indexOf(layer),1);
        }
    }

    /**
     * Layer中移除所有
     * @param layer
     */
    public layerRemoveAllChild(layer: egret.DisplayObjectContainer): void {
        if(layer instanceof BaseSpriteLayer) {
            layer.removeChildren();
        }
        else if(layer instanceof BaseEuiLayer) {
            (<BaseEuiLayer>layer).removeChildren();
        }
    }

    /**
     * 移除所有Layer
     */
    public removeAllLayer(): void {
        while(this.layers.length) {
            var layer: egret.DisplayObjectContainer = this.layers[0];
            this.layerRemoveAllChild(layer);
            this.removeLayer(layer);
        }
    }
    
    
}
